package cn.edu.neusoft.food229.model;

import java.util.List;

import cn.edu.neusoft.food229.bean.ShopBean;
import cn.edu.neusoft.food229.iface.Shopiface;
import cn.edu.neusoft.food229.listener.ShopListener;
import cn.edu.neusoft.food229.service.ShopService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static cn.edu.neusoft.food229.Commen.Commen.BASEURL;

public class ShopModel implements Shopiface {
    private Retrofit retrofit;
    public ShopModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResult( final ShopListener shopListener) {
       ShopService service=retrofit.create(ShopService.class);
        Call<List<ShopBean>> call
                =service.getdata();
        call.enqueue(new Callback<List<ShopBean>>() {
            @Override
            public void onResponse(Call<List<ShopBean>> call, Response<List<ShopBean>> response) {

                if(response.isSuccessful() && response!=null){
                    shopListener.onResponce(response.body());
                }
                else shopListener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<ShopBean>> call, Throwable t) {
                shopListener.onFail(t.toString());

            }
        });


    }
}
